﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using BLL;

public partial class TotalPlan_ProcessedTotalPlan : System.Web.UI.Page
{
    Authentication dllAuthentication = new Authentication();
    Model.UserInfo objUserInfo = new Model.UserInfo();
    protected void Page_Load(object sender, EventArgs e)
    {
        dllAuthentication.isLogin();
        if (!IsPostBack)
        {
            bindDropDownList(this.drpYear);
            
            bindGridView();
        }
    }
    /// <summary>
    /// 绑定下拉列表
    /// </summary>
    /// <param name="_drp"></param>
    void bindDropDownList(DropDownList _drp)
    {
        if (_drp.Equals(this.drpYear))
        {
            for (int i = 2009; i < 2100; i++)
            {
                ListItem li = new ListItem(i.ToString(), i.ToString());
                _drp.Items.Add(li);
            }
            //根据系统日期自动绑定到当前年份
            string _year = DateTime.Now.Year.ToString();
            this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year));
        }
        
    }

    /// <summary>
    /// 绑定Gridview
    /// </summary>
    void bindGridView()
    {
        object[] _value = new object[2];
        _value[0] = this.drpYear.SelectedItem.Text;
        _value[1] = objUserInfo.UserName;
        TotalPlanMgr dllTotalPlanMgr = new TotalPlanMgr();
        //根据年份获得总表计划
        DataTable dt = dllTotalPlanMgr.getTotalPlanProcessed(_value);
        this.gdvTotalPlan.DataSource = dt;
        this.gdvTotalPlan.DataBind();
        for (int i = 0; i < this.gdvTotalPlan.Rows.Count; i++)
        {
            if (this.gdvTotalPlan.DataKeys[i]["AttachmentPath"].ToString().Length == 0)
            {
                Button _btn = (Button)this.gdvTotalPlan.Rows[i].Cells[12].FindControl("btnDownLoad");
                _btn.Visible = false;
                Label _lbl = (Label)this.gdvTotalPlan.Rows[i].Cells[12].FindControl("lblAlert");
                _lbl.Visible = true;
                _lbl.Text = "无附件";
            }
        }
    }


    /// <summary>
    /// 部门改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindGridView();
    }

    /// <summary>
    /// 年份改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindGridView();

    }


    /// <summary>
    /// 全选事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ckbAllSelect_CheckedChanged(object sender, EventArgs e)
    {
        if (this.ckbAllSelect.Checked == true)
        {
            for (int i = 0; i < this.gdvTotalPlan.Rows.Count; i++)
            {
                CheckBox _chk = ((CheckBox)this.gdvTotalPlan.Rows[i].Cells[0].FindControl("ckbSelect"));
                _chk.Checked = true;
            }
        }
        else
        {
            for (int i = 0; i < this.gdvTotalPlan.Rows.Count; i++)
            {
                CheckBox _chk = ((CheckBox)this.gdvTotalPlan.Rows[i].Cells[0].FindControl("ckbSelect"));
                _chk.Checked = false;
            }
        }
    }


    /// <summary>
    /// 流程显示事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnVerifyCircuit_Click(object sender, EventArgs e)
    {
        int _count = 0;
        string _url = "";
        int _totalPlanId = 0;
        for (int i = 0; i < this.gdvTotalPlan.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvTotalPlan.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                _count++;
                _totalPlanId = Convert.ToInt32(this.gdvTotalPlan.DataKeys[i]["TotalPlanId"].ToString());
            }
        }
        if (_count > 1)
        {
            Utility.HttpHelp.alert("只能选择一个计划", Page);
            return;
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请选择一个计划", Page);
            return;
        }
        _url = "../WebCommon/VerifyCircrtForTotalPlan.aspx?PlanId=" + _totalPlanId + "&PlanType=4";
        Page.RegisterStartupScript("windowOpen",
           "<script language=javascript>window.open('" + _url + "','newwindow','height=150,width=550,top=250,left=300,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
    }




    /// <summary>
    /// GridView行帮顶事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvTotalPlan_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            e.Row.Cells[2].Text = e.Row.Cells[2].Text.Replace("&nbsp;", "");
            e.Row.Cells[12].Text = e.Row.Cells[12].Text.Replace("&nbsp;", "");
            e.Row.Cells[2].Text += e.Row.Cells[12].Text;
            e.Row.Cells[12].Visible = false;
        }
    }

    /// <summary>
    /// 下载附件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDownLoad_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button btt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)btt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        //读取隐藏地址列
        string path = this.gdvTotalPlan.DataKeys[i]["AttachmentPath"].ToString();
        FileDownload(path);
    }


    ///   <summary>   
    ///   文件下载   
    ///   </summary>   
    ///   <param   name="FullFileName"></param>   
    private void FileDownload(string strFileName)
    {
        FileInfo DownloadFile = new FileInfo(strFileName);
        if (!DownloadFile.Exists)
        {
            Utility.HttpHelp.alert("无附件,请与管理员联系", Page);
            return;
        }
        Response.Clear();
        Response.ClearHeaders();
        Response.Buffer = false;
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName.Substring(strFileName.LastIndexOf('\\') + 1), System.Text.Encoding.UTF8));
        // Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());

        Response.WriteFile(DownloadFile.FullName);
        Response.Flush();
        Response.End();

    }

}
